from goods.models import GoodsChannel


def category():
    # 1、创建分类
    categories_dict = dict()
    channels = GoodsChannel.objects.order_by("group_id", "sequence")
    for channel in channels:
        one_group = channel.group_id
        if one_group not in categories_dict:
            categories_dict[one_group] = {"channels": [], "sub_cats": []}
        # 一级分类
        categories_dict[one_group]["channels"].append({
            "id": channel.category.id,
            "name": channel.category.name,
            "url": channel.url
        })
        # 二级分类
        categorys = channel.category.subs.all()
        for category in categorys:
            categories_dict[one_group]["sub_cats"].append({
                "id": category.id,
                "name": category.name,
                "sub_cats": []
            })
            # 三级分类
            for i in category.subs.all():
                categories_dict[one_group]["sub_cats"][-1]["sub_cats"].append({
                    "id": i.id,
                    "name": i.name
                })
    return categories_dict
